package com.naiterui.ehp.bs.patient.controller;

import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.support.context.ResourceHolder;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bp.support.security.SettingConfig;
import com.naiterui.ehp.bs.patient.exception.ExceptionCodes;
import com.naiterui.ehp.bs.patient.service.IMedicationRemindService;
import com.naiterui.ehp.bs.patient.service.impl.ApMedicationRemindServiceImpl;
import com.naiterui.ehp.bs.patient.vo.MedicationRemindVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 用药提醒
 *
 * @author fanjd
 * @since 1.0.0
 */
@Api(tags = {"用药提醒", SwaggerTag.PATIENT})
@Controller
@RequestMapping("ap/medication/remind")
public class ApMedicationRemindController {
    private static final Logger LOGGER = LoggerFactory.getLogger(ApMedicationRemindServiceImpl.class);
    @Autowired
    private IMedicationRemindService medicationRemindService;

    /**
     * 患者用药提醒查询
     *
     * @param patientId
     *
     * @return
     *
     * @throws BusinessException
     * @Author fanjd
     * @Date 2018年1月2日
     * @since 1.0.0
     */
    @RequestMapping("/detail")
    @ResponseBody
    @ApiOperation(value = "用药提醒查询", notes = "查询患者的用药提醒", httpMethod = "GET")
    @ApiImplicitParams({@ApiImplicitParam(name = "patientId", value = "患者ID", required = true, dataType = "long", paramType = "query")})
    public ResponseVO<MedicationRemindVO> detail(Long patientId) throws BusinessException {
        if (null == patientId) {
            patientId = (Long) ResourceHolder.getResource(SettingConfig.PATIENT_ID);
        }
        if (patientId == null) {
            LOGGER.info("参数校验失败");
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS, this.medicationRemindService.detail(patientId));
    }

    /**
     * 患者用药提醒新增/更新
     *
     * @param patientId
     *
     * @return
     *
     * @throws BusinessException
     * @Author fanjd
     * @Date 2018年1月2日
     * @since 1.0.0
     */
    @RequestMapping("/update")
    @ResponseBody
    @ApiOperation(value = "用药提醒更新", notes = "根据医患者id更新用药提醒", httpMethod = "POST")
    @ApiImplicitParams({@ApiImplicitParam(name = "patientId", value = "患者id", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "remindTimes", value = "提醒次数", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "remindDays", value = "提醒天数", required = false, dataType = "int", paramType = "query")})
    public ResponseVO<Void> update(Long patientId, Integer remindTimes, Integer remindDays) throws BusinessException {
        if (null == patientId) {
            patientId = (Long) ResourceHolder.getResource(SettingConfig.PATIENT_ID);
        }
        this.medicationRemindService.update(patientId, remindTimes, remindDays, null, null);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }

    /**
     * 患者用药提醒新增/更新
     *
     * @param patientId
     *
     * @return
     *
     * @throws BusinessException
     * @Author fanjd
     * @Date 2018年1月2日
     * @since 1.0.0
     */
    @RequestMapping("/close")
    @ResponseBody
    @ApiOperation(value = "关用药提醒闭", notes = "根据医患者id关闭用药提醒", httpMethod = "POST")
    @ApiImplicitParams({@ApiImplicitParam(name = "patientId", value = "患者id", required = true, dataType = "int", paramType = "query")})
    public ResponseVO<Void> close(Long patientId) throws BusinessException {
        if (null == patientId) {
            patientId = (Long) ResourceHolder.getResource(SettingConfig.PATIENT_ID);
        }
        this.medicationRemindService.removeMedicationRemind(patientId);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }

}
